package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

//启动的初始化任务
@Service
@Slf4j
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class InitTask {
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    StringRedisTemplate redisTemplate;


    //如果一个组件只有有参构造器，spring在创建这个对象的时候，就把构造器需要的组件提前准备好
//    public InitTask(SkuInfoService skuInfoService,
//                    StringRedisTemplate redisTemplate
//                    ){
//        log.info("initTask正在创建对象");
//        this.skuInfoService = skuInfoService;
//        this.redisTemplate = redisTemplate;
//
//
//    }


    @PostConstruct//组件创建好之后干什么
    public void initSkuIdBitMap(){
        log.info("init对象完全创建好以后执行。。。正在准备bitmap数据");
//        redisClient.connect();

//        connection.setBit()
        //查出所有的skuId
        //使用分页查询

        List<Long> ids = skuInfoService.getSkuId();

        //初始化bitmap
        for (Long id : ids) {
            redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP_KEY,id,true);
        }
        log.info("skuids初始化成功");
    }
}
